/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *
 * Copyright (c) 2013-2014 jfinal app. jfapp Group.
 */

package app.models.member;

import app.models.DaoKit;
import com.github.sog.annotation.TableBind;
import com.github.sog.config.StringPool;
import com.github.sog.controller.datatables.core.ColumnDef;
import com.github.sog.controller.datatables.core.DatatablesCriterias;
import com.github.sog.libs.AppFunc;
import com.github.sog.plugin.sqlinxml.SqlKit;
import com.google.common.base.Preconditions;
import com.google.common.base.Strings;
import com.google.common.collect.Lists;
import com.jfinal.plugin.activerecord.Model;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;

import java.util.List;

/**
 * <p>
 * The table ol_member_integral_record mapping model.
 * </p>
 */
@TableBind(tableName = "ol_member_integral_record")
public class MemberIntegralRecord extends Model<MemberIntegralRecord> {

    /**
     * The public dao.
     */
    public static final  MemberIntegralRecord dao              = new MemberIntegralRecord();
    private static final long                 serialVersionUID = 1L;

    public List<MemberIntegralRecord> list() {
        return find(SqlKit.sql("memberintegralrecord.list"));
    }

    public List<MemberIntegralRecord> findByMember(long mid) {
        return find(SqlKit.sql("memberintegralrecord.findByMember"), mid);
    }

    public Page<Record> datatables(DatatablesCriterias criterias, String date, Member member) {
        Preconditions.checkNotNull(member);
        final List<Object> params = Lists.newArrayList();
        params.add(member.get("id"));
        String where = SqlKit.sql("memberintegralrecord.where");
        if (!Strings.isNullOrEmpty(date)) {
            where += " AND record_time BETWEEN ? AND ?";
            params.add(date + " 00:00:00");
            params.add(date + " 23:59:59");
        }
        return DaoKit.paginate(where, SqlKit.sql("memberintegralrecord.columns"), criterias, " ORDER BY record_time DESC", params);

    }

    public Page<Record> datatablesByMember(DatatablesCriterias criterias, Member member) {
        Preconditions.checkNotNull(member);
        final List<Object> params = Lists.newArrayList();
        params.add(member.getNumber(AppFunc.TABLE_PK_COLUMN));
        return DaoKit.paginate("memberintegralrecord", criterias, params);
    }

    public Page<Record> customerRecods(DatatablesCriterias criterias, Member member, String recordDate) {
        final List<Object> params = Lists.newArrayList();
        params.add(false);
        String where = SqlKit.sql("memberintegralrecord.customerRecodsWhere");

        if (member != null) {
            String memberName = member.getStr("name");
            if (!Strings.isNullOrEmpty(memberName)) {
                where += " AND m.name like ?";
                params.add(StringPool.PERCENT + memberName + StringPool.PERCENT);
            }

            String card_no = member.getStr("card_no");
            if (!Strings.isNullOrEmpty(card_no)) {
                where += " AND m.card_no like ?";
                params.add(StringPool.PERCENT + card_no + StringPool.PERCENT);
            }

            String phone = member.getStr("phone");
            if (!Strings.isNullOrEmpty(phone)) {
                where += " AND m.phone like ?";
                params.add(StringPool.PERCENT + phone + StringPool.PERCENT);
            }
        }

        if (!Strings.isNullOrEmpty(recordDate)) {
            where += " AND r.record_time BETWEEN ? AND ?";
            params.add(recordDate + " 00:00:00");
            params.add(recordDate + " 23:59:59");
        }
        return DaoKit.paginate(where, SqlKit.sql("memberintegralrecord.customerRecodsColumns"), criterias, " ORDER BY r.record_time DESC", params);
    }
}